package gui.QualityControl;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.TitledBorder;

import entities.FailSet;
import entities.RepolishSet;
import net.miginfocom.swing.MigLayout;

import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.SystemColor;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.Font;

/**
 * @author Idan
 * This is the report GUI used for Presbia orders
 */
public class QCPresbiaReport extends JPanel
{
	private JTextField c1TextField;
	private JTextField c2TextField;
	private JTextField c3TextField;
	private JTextField c4TextField;
	private JTextField c5TextField;
	private JTextField c6TextField;
	private JTextField c7TextField;
	private JTextField c8TextField;
	private JTextField c9TextField;
	private JTextField c10TextField;
	private JTextField c11TextField;
	private JTextField c12TextField;
	private JTextField c13TextField;
	private JTextField c14TextField;
	private JTextField c15TextField;
	private JTextField c16TextField;
	private JTextField dryFailsTextField;
	private JTextArea remarksTextArea;
	public QCPresbiaReport() {
		setBackground(new Color(173, 216, 230));
		setLayout(new MigLayout("", "[grow,left][][][grow,right]", "[grow,top][][][grow,bottom]"));
		
		JPanel panel = new JPanel(){@Override
			public void paintComponent(Graphics g) {
	        Graphics2D g2d = (Graphics2D) g;
	        Color color1 = getBackground();
	        Color color2 = color1.darker();
	        int w = getWidth();
	        int h = getHeight(); 
	        GradientPaint gp = new GradientPaint(
	                0, 0, color1,
	                0, h, color2);

	        g2d.setPaint(gp);
	        g2d.fillRect(0, 0, w, h);
	        }};
		panel.setBackground(new Color(173, 216, 230));
		panel.setBorder(new TitledBorder(null, " \u05E4\u05E1\u05D9\u05DC\u05D5\u05EA", TitledBorder.RIGHT, TitledBorder.TOP, null, new Color(59, 59, 59)));
		add(panel, "cell 2 1 1 2,grow");
		panel.setLayout(new MigLayout("", "[][][grow][][][grow][][]", "[grow,top][][][][][][][][][][][grow,bottom]"));
		
		JLabel lblC = new JLabel("C1 - Turn Lens:");
		lblC.setFont(new Font("Tahoma", Font.PLAIN, 13));
		panel.add(lblC, "cell 1 1,alignx left");
		
		c1TextField = new JTextField();
		c1TextField.setFont(new Font("Tahoma", Font.PLAIN, 13));
		c1TextField.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e) {((JTextField)(e.getComponent())).selectAll();
			}
		});
		c1TextField.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				c2TextField.requestFocus();
			}
		});
		c1TextField.setText("0");
		c1TextField.setHorizontalAlignment(SwingConstants.CENTER);
		panel.add(c1TextField, "cell 2 1,alignx left");
		c1TextField.setColumns(3);
		
		JLabel lblCLost = new JLabel("C9 - Lost Lens:");
		lblCLost.setFont(new Font("Tahoma", Font.PLAIN, 13));
		panel.add(lblCLost, "cell 4 1,alignx left");
		
		c9TextField = new JTextField();
		c9TextField.setFont(new Font("Tahoma", Font.PLAIN, 13));
		c9TextField.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e) {((JTextField)(e.getComponent())).selectAll();
			}
		});
		c9TextField.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				c10TextField.requestFocus();
			}
		});
		c9TextField.setText("0");
		c9TextField.setHorizontalAlignment(SwingConstants.CENTER);
		c9TextField.setColumns(3);
		panel.add(c9TextField, "cell 5 1,growx");
		
		JLabel lblCChipped = new JLabel("C2 - Chipped Edge:");
		lblCChipped.setFont(new Font("Tahoma", Font.PLAIN, 13));
		panel.add(lblCChipped, "cell 1 2,alignx left");
		
		c2TextField = new JTextField();
		c2TextField.setFont(new Font("Tahoma", Font.PLAIN, 13));
		c2TextField.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e) {((JTextField)(e.getComponent())).selectAll();
			}
		});
		c2TextField.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				c3TextField.requestFocus();
			}
		});
		c2TextField.setText("0");
		c2TextField.setHorizontalAlignment(SwingConstants.CENTER);
		c2TextField.setColumns(3);
		panel.add(c2TextField, "cell 2 2,growx");
		
		JLabel lblCLathe = new JLabel("C10 - Lathe Marks:");
		lblCLathe.setFont(new Font("Tahoma", Font.PLAIN, 13));
		panel.add(lblCLathe, "cell 4 2,alignx left");
		
		c10TextField = new JTextField();
		c10TextField.setFont(new Font("Tahoma", Font.PLAIN, 13));
		c10TextField.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e) {((JTextField)(e.getComponent())).selectAll();
			}
		});
		c10TextField.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) 
			{
				c11TextField.requestFocus();
			}
		});
		c10TextField.setText("0");
		c10TextField.setHorizontalAlignment(SwingConstants.CENTER);
		c10TextField.setColumns(3);
		panel.add(c10TextField, "cell 5 2,growx");
		
		JLabel lblCCutting = new JLabel("C3 - Cutting Chips:");
		lblCCutting.setFont(new Font("Tahoma", Font.PLAIN, 13));
		panel.add(lblCCutting, "cell 1 3,alignx left");
		
		c3TextField = new JTextField();
		c3TextField.setFont(new Font("Tahoma", Font.PLAIN, 13));
		c3TextField.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e) {((JTextField)(e.getComponent())).selectAll();
			}
		});
		c3TextField.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) 
			{
				c4TextField.requestFocus();
			}
		});
		c3TextField.setText("0");
		c3TextField.setHorizontalAlignment(SwingConstants.CENTER);
		c3TextField.setColumns(3);
		panel.add(c3TextField, "cell 2 3,growx");
		
		JLabel lblCChipped_1 = new JLabel("C11 - Chipped Hole:");
		lblCChipped_1.setFont(new Font("Tahoma", Font.PLAIN, 13));
		panel.add(lblCChipped_1, "cell 4 3,alignx left");
		
		c11TextField = new JTextField();
		c11TextField.setFont(new Font("Tahoma", Font.PLAIN, 13));
		c11TextField.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e) {((JTextField)(e.getComponent())).selectAll();
			}
		});
		c11TextField.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				c12TextField.requestFocus();
			}
		});
		c11TextField.setText("0");
		c11TextField.setHorizontalAlignment(SwingConstants.CENTER);
		c11TextField.setColumns(3);
		panel.add(c11TextField, "cell 5 3,growx");
		
		JLabel lblCScratch = new JLabel("C4 - Scratch:");
		lblCScratch.setFont(new Font("Tahoma", Font.PLAIN, 13));
		panel.add(lblCScratch, "cell 1 4,alignx left");
		
		c4TextField = new JTextField();
		c4TextField.setFont(new Font("Tahoma", Font.PLAIN, 13));
		c4TextField.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e) {((JTextField)(e.getComponent())).selectAll();
			}
		});
		c4TextField.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				c5TextField.requestFocus();
			}
		});
		c4TextField.setText("0");
		c4TextField.setHorizontalAlignment(SwingConstants.CENTER);
		c4TextField.setColumns(3);
		panel.add(c4TextField, "cell 2 4,growx");
		
		JLabel lblCNo = new JLabel("C12 - No Hole:");
		lblCNo.setFont(new Font("Tahoma", Font.PLAIN, 13));
		panel.add(lblCNo, "cell 4 4,alignx left");
		
		c12TextField = new JTextField();
		c12TextField.setFont(new Font("Tahoma", Font.PLAIN, 13));
		c12TextField.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e) {((JTextField)(e.getComponent())).selectAll();
			}
		});
		c12TextField.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) 
			{
				c13TextField.requestFocus();
			}
		});
		c12TextField.setText("0");
		c12TextField.setHorizontalAlignment(SwingConstants.CENTER);
		c12TextField.setColumns(3);
		panel.add(c12TextField, "cell 5 4,growx");
		
		JLabel lblCPrism = new JLabel("C5 - Prism:");
		lblCPrism.setFont(new Font("Tahoma", Font.PLAIN, 13));
		panel.add(lblCPrism, "cell 1 5,alignx left");
		
		c5TextField = new JTextField();
		c5TextField.setFont(new Font("Tahoma", Font.PLAIN, 13));
		c5TextField.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e) {((JTextField)(e.getComponent())).selectAll();
			}
		});
		c5TextField.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				c6TextField.requestFocus();
			}
		});
		c5TextField.setText("0");
		c5TextField.setHorizontalAlignment(SwingConstants.CENTER);
		c5TextField.setColumns(3);
		panel.add(c5TextField, "cell 2 5,growx");
		
		JLabel lblCUndefined = new JLabel("C13 - Undefined Particles:");
		lblCUndefined.setFont(new Font("Tahoma", Font.PLAIN, 13));
		panel.add(lblCUndefined, "cell 4 5,alignx left");
		
		c13TextField = new JTextField();
		c13TextField.setFont(new Font("Tahoma", Font.PLAIN, 13));
		c13TextField.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e) {((JTextField)(e.getComponent())).selectAll();
			}
		});
		c13TextField.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				c14TextField.requestFocus();
			}
		});
		c13TextField.setText("0");
		c13TextField.setHorizontalAlignment(SwingConstants.CENTER);
		c13TextField.setColumns(3);
		panel.add(c13TextField, "cell 5 5,growx");
		
		JLabel lblCFolded = new JLabel("C6 - Folded Lens:");
		lblCFolded.setFont(new Font("Tahoma", Font.PLAIN, 13));
		panel.add(lblCFolded, "cell 1 6,alignx left");
		
		c6TextField = new JTextField();
		c6TextField.setFont(new Font("Tahoma", Font.PLAIN, 13));
		c6TextField.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e) {((JTextField)(e.getComponent())).selectAll();
			}
		});
		c6TextField.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				c7TextField.requestFocus();
			}
		});
		c6TextField.setText("0");
		c6TextField.setHorizontalAlignment(SwingConstants.CENTER);
		c6TextField.setColumns(3);
		panel.add(c6TextField, "cell 2 6,growx");
		
		JLabel lblCFoggy = new JLabel("C14 - Foggy Resolution:");
		lblCFoggy.setFont(new Font("Tahoma", Font.PLAIN, 13));
		panel.add(lblCFoggy, "cell 4 6,alignx left");
		
		c14TextField = new JTextField();
		c14TextField.setFont(new Font("Tahoma", Font.PLAIN, 13));
		c14TextField.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e) {((JTextField)(e.getComponent())).selectAll();
			}
		});
		c14TextField.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				c15TextField.requestFocus();
			}
		});
		c14TextField.setText("0");
		c14TextField.setHorizontalAlignment(SwingConstants.CENTER);
		c14TextField.setColumns(3);
		panel.add(c14TextField, "cell 5 6,growx");
		
		JLabel lblCOod = new JLabel("C7 - OOD Diameter:");
		lblCOod.setFont(new Font("Tahoma", Font.PLAIN, 13));
		panel.add(lblCOod, "cell 1 7,alignx left");
		
		c7TextField = new JTextField();
		c7TextField.setFont(new Font("Tahoma", Font.PLAIN, 13));
		c7TextField.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e) {((JTextField)(e.getComponent())).selectAll();
			}
		});
		c7TextField.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				c8TextField.requestFocus();
			}
		});
		c7TextField.setText("0");
		c7TextField.setHorizontalAlignment(SwingConstants.CENTER);
		c7TextField.setColumns(3);
		panel.add(c7TextField, "cell 2 7,growx");
		
		JLabel lblCOod_2 = new JLabel("C15 - OOD Diopter Power:");
		lblCOod_2.setFont(new Font("Tahoma", Font.PLAIN, 13));
		panel.add(lblCOod_2, "cell 4 7,alignx left");
		
		c15TextField = new JTextField();
		c15TextField.setFont(new Font("Tahoma", Font.PLAIN, 13));
		c15TextField.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e) {((JTextField)(e.getComponent())).selectAll();
			}
		});
		c15TextField.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				c16TextField.requestFocus();
			}
		});
		c15TextField.setText("0");
		c15TextField.setHorizontalAlignment(SwingConstants.CENTER);
		c15TextField.setColumns(3);
		panel.add(c15TextField, "cell 5 7,growx");
		
		JLabel lblCOod_1 = new JLabel("C8 - OOD Thickness:");
		lblCOod_1.setFont(new Font("Tahoma", Font.PLAIN, 13));
		panel.add(lblCOod_1, "cell 1 8,alignx left");
		
		c8TextField = new JTextField();
		c8TextField.setFont(new Font("Tahoma", Font.PLAIN, 13));
		c8TextField.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e) {((JTextField)(e.getComponent())).selectAll();
			}
		});
		c8TextField.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				c9TextField.requestFocus();
			}
		});
		c8TextField.setText("0");
		c8TextField.setHorizontalAlignment(SwingConstants.CENTER);
		c8TextField.setColumns(3);
		panel.add(c8TextField, "cell 2 8,growx");
		
		JLabel lblCOod_3 = new JLabel("C16 - OOD Plano Power:");
		lblCOod_3.setFont(new Font("Tahoma", Font.PLAIN, 13));
		panel.add(lblCOod_3, "cell 4 8,alignx left");
		
		c16TextField = new JTextField();
		c16TextField.setFont(new Font("Tahoma", Font.PLAIN, 13));
		c16TextField.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e) {((JTextField)(e.getComponent())).selectAll();
			}
		});
		c16TextField.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) 
			{
				dryFailsTextField.requestFocus();
			}
		});
		c16TextField.setText("0");
		c16TextField.setHorizontalAlignment(SwingConstants.CENTER);
		c16TextField.setColumns(3);
		panel.add(c16TextField, "cell 5 8,growx");
		
		dryFailsTextField = new JTextField();
		dryFailsTextField.setFont(new Font("Tahoma", Font.PLAIN, 13));
		dryFailsTextField.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e) {((JTextField)(e.getComponent())).selectAll();
			}
		});
		dryFailsTextField.setText("0");
		dryFailsTextField.setHorizontalAlignment(SwingConstants.CENTER);
		dryFailsTextField.setColumns(3);
		panel.add(dryFailsTextField, "flowx,cell 4 10,alignx center");
		
		JLabel label = new JLabel("פסולים ביבש:");
		label.setFont(new Font("Tahoma", Font.PLAIN, 13));
		panel.add(label, "cell 4 10,alignx center");
		
		JPanel remarksPanel = new JPanel(){@Override
			public void paintComponent(Graphics g) {
	        Graphics2D g2d = (Graphics2D) g;
	        Color color1 = getBackground().darker();
	        Color color2 = color1.darker();
	        int w = getWidth();
	        int h = getHeight(); 
	        GradientPaint gp = new GradientPaint(
	                0, 0, color1,
	                0, h, color2);

	        g2d.setPaint(gp);
	        g2d.fillRect(0, 0, w, h);
	        }};
		remarksPanel.setBackground(new Color(173, 216, 230));
		remarksPanel.setBorder(new TitledBorder(null, "\u05D4\u05E2\u05E8\u05D5\u05EA \u05DC\u05D3\u05D5\u05D7", TitledBorder.RIGHT, TitledBorder.TOP, null, null));
		add(remarksPanel, "cell 1 2,growx,aligny bottom");
		remarksPanel.setLayout(new MigLayout("", "[grow][:216px:216px][grow]", "[][120px:n:216px,grow][]"));
		
		JLabel lblNewLabel = new JLabel("טקסט חופשי:");
		lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 13));
		remarksPanel.add(lblNewLabel, "cell 1 0,alignx center");
		
		JScrollPane scrollPane = new JScrollPane();
		remarksPanel.add(scrollPane, "cell 1 1,grow");
		
		remarksTextArea = new JTextArea();
		remarksTextArea.setFont(new Font("Courier New", Font.PLAIN, 13));
		scrollPane.setViewportView(remarksTextArea);
	}

	/**
	 * Create fail report for the order
	 */

	public FailSet getFailSet() 
	{
		FailSet report = new FailSet();
		report.setC1TurnLens(c1TextField.getText());
		report.setC2ChippedEdge(c2TextField.getText());
		report.setC3CuttingChips(c3TextField.getText());
		report.setC4Scratches(c4TextField.getText());
		report.setC5Prism(c5TextField.getText());
		report.setC6FoldedLens(c6TextField.getText());
		report.setC7OODDiameter(c7TextField.getText());
		report.setC8OODThickness(c8TextField.getText());
		report.setC9Lost(c9TextField.getText());
		report.setC10LatheMarks(c10TextField.getText());
		report.setC11ChippedHole(c11TextField.getText());
		report.setC12NoHole(c12TextField.getText());
		report.setC13UndefinedParticles(c13TextField.getText());
		report.setC14FoggyResolution(c14TextField.getText());
		report.setC15OODDiopterPower(c15TextField.getText());
		report.setC16OODPlanoPower(c16TextField.getText());
		report.setDryFails(dryFailsTextField.getText());
		
		
		report.setComments(remarksTextArea.getText());
		
		int sumOfFails = 	Integer.parseInt(c1TextField.getText()) +
							Integer.parseInt(c2TextField.getText()) +
							Integer.parseInt(c3TextField.getText()) +
							Integer.parseInt(c4TextField.getText()) +
							Integer.parseInt(c5TextField.getText()) +
							Integer.parseInt(c6TextField.getText()) +
							Integer.parseInt(c7TextField.getText()) +
							Integer.parseInt(c8TextField.getText()) +
							Integer.parseInt(c9TextField.getText()) +
							Integer.parseInt(c10TextField.getText()) +
							Integer.parseInt(c11TextField.getText()) +
							Integer.parseInt(c12TextField.getText()) +
							Integer.parseInt(c13TextField.getText()) +
							Integer.parseInt(c14TextField.getText()) +
							Integer.parseInt(c15TextField.getText()) +
							Integer.parseInt(c16TextField.getText()) +
							Integer.parseInt(dryFailsTextField.getText());
															
		
		report.setFailsetFlag(sumOfFails);
		return report;
	}
	
	/**
	 * Create repolish report for the order
	 */
	
	public RepolishSet getRepolishSet()
	{
		RepolishSet report = new RepolishSet();
	
		report.setRepolishFlag(0);	
		return report;
	}


/**
 * Test for input validity
 */
	public boolean everythingIsValid()
	{
		return (
				utilities.Services.isNumeric(c1TextField.getText()) &&
				utilities.Services.isNumeric(c2TextField.getText()) &&
				utilities.Services.isNumeric(c3TextField.getText()) &&
				utilities.Services.isNumeric(c4TextField.getText()) &&
				utilities.Services.isNumeric(c5TextField.getText()) &&
				utilities.Services.isNumeric(c6TextField.getText()) &&
				utilities.Services.isNumeric(c7TextField.getText()) &&
				utilities.Services.isNumeric(c8TextField.getText()) &&
				utilities.Services.isNumeric(c9TextField.getText()) &&
				utilities.Services.isNumeric(c10TextField.getText()) &&
				utilities.Services.isNumeric(c11TextField.getText()) &&
				utilities.Services.isNumeric(c12TextField.getText()) &&
				utilities.Services.isNumeric(c13TextField.getText()) &&
				utilities.Services.isNumeric(c14TextField.getText()) &&
				utilities.Services.isNumeric(c15TextField.getText()) &&
				utilities.Services.isNumeric(c16TextField.getText()) &&
				utilities.Services.isNumeric(dryFailsTextField.getText())
				);
	
	}


/**
 * Clear GUI fields
 */
	public void clearFields()
	{
		// Quality
		c1TextField.setText("0");
		c2TextField.setText("0");
		c3TextField.setText("0");
		c4TextField.setText("0");
		c5TextField.setText("0");
		c6TextField.setText("0");
		c7TextField.setText("0");
		c8TextField.setText("0");
		c9TextField.setText("0");
		c10TextField.setText("0");
		c11TextField.setText("0");
		c12TextField.setText("0");
		c13TextField.setText("0");
		c14TextField.setText("0");
		c15TextField.setText("0");
		c16TextField.setText("0");
		dryFailsTextField.setText("0");
		
		// Comments
		remarksTextArea.setText("");
		
	}
	
	
	@Override
	public void paintComponent(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    Color color1 = getBackground();
    Color color2 = color1.darker();
    int w = getWidth();
    int h = getHeight(); 
    GradientPaint gp = new GradientPaint(
            0, 0, color1,
            0, h, color2);

    g2d.setPaint(gp);
    g2d.fillRect(0, 0, w, h);
    }
}
